package com.kuang.bascic.service;

import com.kuang.bascic.Myexception.BusinessException;
import com.kuang.bascic.utils.VerifyCodeUtils;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Service

public class IVerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String getImgCode(String key) {
        //空字符校验
        if (StringUtil.isNullOrEmpty(key)){
            throw new BusinessException("验证码为空");
        }
        //生成产生随机验证码
        String code = VerifyCodeUtils.generateVerifyCode(4);
        //存放redis
        redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);
        // 返回base64字符串
        return VerifyCodeUtils.VerifyCode(110,50, code);

    }
}

